﻿using System;
using Microsoft.Exchange.WebServices.Autodiscover;
using Microsoft.Exchange.WebServices.Data;

namespace JL.LiveAtEdu.Helpers
{
    public class Bind
    {
        #region ExchangeService Binding
        public static ExchangeService MeIn()
        {
            try
            {
                //Create Service
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
                {
                    /*Assign Credentials*/
                    Credentials = new WebCredentials(MyLiveUser, MyLivePassword)
                };
                // Create an instance of the AutodiscoverService.
                AutodiscoverService ads = new AutodiscoverService
                {
                    /* Enable tracing.*/
                    TraceEnabled = true,
                    /* Set the credentials.*/
                    Credentials = service.Credentials,
                    /* Prevent the AutodiscoverService from */
                    /*looking in the local Active Directory*/
                    /* for the Exchange Web Services Services SCP.*/
                    EnableScpLookup = false, /* Specify a redirection URL validation */
                    /*callback that returns true for valid URLs.*/
                    RedirectionUrlValidationCallback = RedirectionUrlValidationCallback
                };
                // Get the Exchange Web Services URL for the user's mailbox.
                GetUserSettingsResponse response =
                ads.GetUserSettings(MyLiveUser, UserSettingName.ExternalEwsUrl);
                // Extract the Exchange Web Services URL from the response.
                var externalEwsUrl = new
                Uri(response.Settings[UserSettingName.ExternalEwsUrl].ToString());
                // Set the URL of the ExchangeService object.
                service.Url = externalEwsUrl;
                return service;
            }
            catch (AutodiscoverRemoteException)
            {
                return null;
            }
        }

        static bool RedirectionUrlValidationCallback(String redirectionUrl)
        {
            // The default for the validation callback is to reject the URL.
            bool result;

            Uri redirectionUri = new Uri(redirectionUrl);

            // Validate the contents of the redirection URL. 
            //In this simple validation
            // callback, the redirection URL is considered 
            //valid if it is using HTTPS
            // to encrypt the authentication credentials. 
            if (redirectionUri.Scheme == "https")
                result = true;
            else
                result = false;
            return result;
        }
        #endregion

        #region Public Properties

        public static string MyLiveUser { get; set; }
        public static string MyLivePassword { get; set; }
        #endregion
    }
}
